{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6a62f3b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "alist = [2, 3, 4, 5, 2, 4, 1, \"a string\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fd9c44ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 4, 5, 2, 4, 1, 'a string']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3c253ad2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5, 2, 4, 1, 'a string']\n"
     ]
    }
   ],
   "source": [
    "print(alist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c066e35c",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list expected at most 1 arguments, got 8",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_9124/614879238.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0maList\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"a string\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: list expected at most 1 arguments, got 8"
     ]
    }
   ],
   "source": [
    "aList = list(2, 3, 4, 5, 2, 4, 1, \"a string\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d11d658f",
   "metadata": {},
   "outputs": [],
   "source": [
    "aList = list([2, 3, 4, 5, 2, 4, 1, \"a string\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c920e335",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list expected at most 1 arguments, got 2",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_9124/2151105391.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0malist\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: list expected at most 1 arguments, got 2"
     ]
    }
   ],
   "source": [
    "alist = list(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5a266b0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['j', 's', 'd', 'v', 'b', ' ', 'k', 'n', 'f', 'j', 'k']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = list(\"jsdvb knfjk\")\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9f302bf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 's', 'b', 'a', 's']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in \"asbas\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e0c696ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6, 8, 10, 12, 14, 16, 18]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[y*2 for y in range(1,10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8e6b9e5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d3c13f8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1249380519304"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "14640489",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['j', 's', 'd', 'v', 'b', ' ', 'k', 'n', 'f', 'j', 'k']\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3b336f19",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.append(\"da\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6a5938f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['j', 's', 'd', 'v', 'b', ' ', 'k', 'n', 'f', 'j', 'k', 'da']"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b8e1f0fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2033054903368"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0132f10b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2033054709192"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [30, 40, 50]\n",
    "b = [60, 70]\n",
    "a.extend(b)\n",
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "73cee242",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[30, 40, 50, 60, 70]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a2220f86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [10 ,20, 30, 20 ,39, 39, 10, 30, 20]\n",
    "a.index(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d072f3d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(20, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3335b322",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.index(20,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "077fc0c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 80, 30, 20, 39, 39, 10, 30, 20]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1]=80\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db79e236",
   "metadata": {},
   "source": [
    "del 删除列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "448cb43e",
   "metadata": {},
   "outputs": [],
   "source": [
    "del a[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "da14e604",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 30, 20, 39, 39, 10, 30, 20]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "259f2f05",
   "metadata": {},
   "source": [
    "del 删除列表本身"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b5090d64",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_16020/3125231900.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "del a\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f0ba902e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2, 3, 4, 5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3d63338",
   "metadata": {},
   "source": [
    "pop（）删除元素，并弹出删除的元素，若未指定下标则删除最后一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b5408997",
   "metadata": {},
   "outputs": [],
   "source": [
    "c = a.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6a591e74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 4, 5]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9b56a84b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 5]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.remove(4)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67a3e6ff",
   "metadata": {},
   "source": [
    "copy()赋值，创建新副本，把值赋给新内存空间，双方内存空间不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "65e1a39d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([10, 20, 30], [10, 500, 30])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aa=[10,20,30]\n",
    "bb=aa.copy()\n",
    "bb[1]=500\n",
    "aa,bb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2fe1fd5",
   "metadata": {},
   "source": [
    "=赋值，双方内存空间一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7ddf6582",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([10, 500, 30], [10, 500, 30])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aa=[10,20,30]\n",
    "bb=aa\n",
    "bb[1]=500\n",
    "aa,bb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "50778159",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([10, 20, 30], [10, 500, 30])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aa=[10,20,30]\n",
    "bb=aa[:]\n",
    "bb[1]=500\n",
    "aa,bb"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b330aa5",
   "metadata": {},
   "source": [
    "sort(reverse=True)逆序,修改原列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "532341bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[53, 44, 34, 23, 20, 12, 11]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [11, 20, 44, 53, 12, 34, 23]\n",
    "a.sort(reverse=True)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f28b503",
   "metadata": {},
   "source": [
    "sorted()正序，不会修改原列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "debd256b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([11, 12, 20, 23, 34, 44, 53], [53, 44, 34, 23, 20, 12, 11])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(a), a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52d3117f",
   "metadata": {},
   "source": [
    "先将元素变为负值后进行正序排序，之后把负值还原成正值输出结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "435a3aeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[53, 44, 34, 23, 20, 12, 11]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(a, key=lambda x:-x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_test",
   "language": "python",
   "name": "my_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
